// release/chatroom/index.js
const db = wx.cloud.database()
const timeutil =require('../../utils/timeutil')
var musicwatcher = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    InputBottom: 0,
    showActionsheet: false,
    disac:false,
    songInfo:{},
    groups: [{
        text: '点歌',
        value: 1
      },
      {
        text: '切歌',
        value: 2
      }
    ],
    content:'',
    chatlist:[],
  },
  getinput(e){
    this.setData({
      content:e.detail.value
    })
  },
  submit(){
    //提交
    let userInfo = wx.getStorageSync('userInfo')
    wx.cloud.callFunction({
      name:'chat',
      data:{
        content:this.data.content,
        type:'text',
        date:timeutil.TimeCode(),
        userInfo
      },
      success:res=>{
        if(res.result.code == 300){
          wx.showToast({
            title: res.result.msg,
            icon:'none'
          })
        }
        this.setData({
          content:''
        })
        console.log(res)
      },
      fail:res=>{
        console.log(res)
      }
    })
  },
  showaction(){
    this.setData({
      showActionsheet:true
    })
  },
  close: function () {
    this.setData({
      showActionsheet: false
    })
  },
  btnClick(e) {
    var that = this;
    console.log(e)
    let sid = e.detail.value
    if(sid == 1){
      wx.navigateTo({
        url: '../musicorder/musicorder',
      })
    }else if(sid == 2){
      console.log('ACC音乐')
      // console.log(this.data.songInfo)
      if(this.data.disac){
        wx.showToast({
          title: '请等待一下',
        })
      }else{
        wx.cloud.callFunction({
          name:'accplayer',
          data:{
            docid:this.data.songInfo._id
          },
          success:res=>{
            console.log(res)
            wx.showToast({
              title: '成功加速60s',
              icon:'none'
            })
            that.setData({
              disac:true
            })
            setTimeout(function(){
              that.setData({
                disac:true
              })
            },60*1000)
          },
          fail:res=>{
            console.log(res)
          }
        })
      }
      
    }
    
    this.close()
  },
  InputFocus(e) {
    this.setData({
      InputBottom: e.detail.height
    })
  },
  InputBlur(e) {
    this.setData({
      InputBottom: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //进入就直接查询正在播放的歌曲 -- 后台数据库监听器可以配置为 30s 一次/ 60s 一次
    var that = this;
    //查询最近的聊天记录
    // db.collection('CHAT_LOG').where({
    //   roomid:1
    // }).orderBy('date','desc').limit(20).get({
    //   success:res=>{
    //     console.log(res)
    //     this.setData({
    //       chatlist:res.data
    //     })
    //   }
    // })
    //监听播放器信息变动
    this.musicwatcher = db.collection('MUSIC_LOG').where({
      playstatus:1
    }).watch({
      onChange: function (snapshot) {
        //只打印变动的信息
        // console.log(snapshot.docChanges)
        if (snapshot.docChanges.length != 0) {
          // console.log(snapshot.docChanges)
          that.setData({
            songInfo: snapshot.docChanges[0].doc
          })
        }
      },
      onError: function (err) {
        console.error('the watch closed because of error', err)
      }
    })
    //监听聊天信息变动
    db.collection('CHAT_LOG').where({
      roomid:1
    }).watch({
      onChange: function (snapshot) {
        //只打印变动的信息
        // console.log(snapshot)
        if (snapshot.docChanges.length != 0) {
          console.log(snapshot.docChanges)
          let tarr = []
          snapshot.docChanges.forEach(function(ele,index){
            tarr.push(ele.doc)
          })
          that.setData({
            chatlist: that.data.chatlist.concat(tarr)
          })
        }
      },
      onError: function (err) {
        console.error('the watch closed because of error', err)
      }
    })

    //
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //初始化欢迎信息
    let userInfo = wx.getStorageSync('userInfo')
    wx.cloud.callFunction({
      name:'chat',
      data:{
        content:'欢迎'+userInfo.nickName+'进入自习室',
        type:'notice',
        date:timeutil.TimeCode(),
        userInfo
      },
      success:res=>{
        this.setData({
          content:''
        })
        console.log(res)
      },
      fail:res=>{
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('隐藏页面')
    this.musicwatcher.close()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})